package com.java.sjzl.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.java.sjzl.entity.User;
import com.java.sjzl.service.IUserService;
import com.java.sjzl.util.CommonUtil;
import com.java.sjzl.util.ExcelUtil;
import com.java.sjzl.vo.UserExportVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huoqiang
 * @description
 * @date 2021/6/23
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@Api(tags = "User控制器")
public class UserController {

    private final IUserService userService;


    @PostMapping("/importExcel")
    public String importExcel(@RequestParam("file") MultipartFile file) {
        try {
            String result = userService.importExcel(file);
            return result;
        } catch (Exception e) {
            return "导入失败";
        }
    }

    @GetMapping("/exportExcel")
    @ApiOperation("exportExcel")
    public void export(HttpServletResponse response) {
        //查询要导出的数据
        List<User> users = userService.getUserExportList();
        ModelMapper modelMapper = new ModelMapper();
        List<UserExportVO> userExportVOs = users.stream()
                .map(user -> modelMapper.map(user, UserExportVO.class))
                .collect(Collectors.toList());
        ExcelUtil.exportExcelX(userExportVOs, "测试导出表", "sheet1", UserExportVO.class, "测试导出表.xlsx", response);
    }

    @GetMapping("/exportExcel2")
    @ApiOperation("exportExcel2")
    public void exportExcel2(HttpServletResponse response) {
        List<Object> users = CommonUtil.generateDummyData(10000);
        ExcelUtil.exportExcelX(users, "测试导出表", "sheet1", UserExportVO.class, "测试导出表.xlsx", response);
    }

    /**
     * 大数据导出2.0
     * /demo/exportExcel5
     * @param response
     */
    @GetMapping("/exportExcel5")
    @ApiOperation("大数据导出2.0")
    public void exportExcel5(HttpServletResponse response) throws IOException {
        log.info("开始导出大数据导出测试.xlsx");
        Date start = new Date();
        Workbook workbook = null;
        ExportParams params = new ExportParams("大数据测试", "测试");
        workbook = ExcelExportUtil.exportBigExcel(params, UserExportVO.class, new MyExcelExportServer(), 100);
        ExcelUtil.downLoadExcel("大数据导出测试.xlsx", response, workbook);
        System.out.println("耗时：" + (new Date().getTime() - start.getTime()) / 1000 + "秒");
    }

    private static class MyExcelExportServer implements IExcelExportServer {
        @Override
        public List<Object> selectListForExcelExport(Object obj, int page) {
            if (((int) obj) == page) {
                return null;
            }
            return generateTestData(10000);
        }

        private List<Object> generateTestData(int dataSize) {
//            List<Object> list = new ArrayList<>();
//            for (int i = 0; i < dataSize; i++) {
//                list.add(new UserExportVO("悟纤-" + i, 1, new Date(), "18688888888", "abc" + i + "@qq.com", null, "公众号SpringBoot"));
//            }
            List<Object> users = CommonUtil.generateDummyData(dataSize);
            return users;
        }
    }



}
